package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.KaoQinMapper;
import com.hqyj.dao.StudentMapper;
import com.hqyj.pojo.KaoQin;
import com.hqyj.pojo.KaoQin;
import com.hqyj.pojo.Student;
import com.hqyj.service.KaoQinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class KaoQinServiceImpl implements KaoQinService {

    @Autowired
    StudentMapper studentMapper;

    //查询
    @Autowired
    KaoQinMapper kaoQinMapper;
    @Override
    public HashMap<String, Object> selectAll(KaoQin kaoQin) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //1 设置分页参数：页码和条数
        PageHelper.startPage(kaoQin.getPage(),kaoQin.getRow());
        //2 查询结果集合
        List<KaoQin> list = kaoQinMapper.selectAll();
        for (KaoQin qin : list) {

            SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" );
            String qTime = sdf1.format(qin.getqTime());
            qin.getStudent().setsSex(qTime);
        }
        // 3 创建分页对象
        PageInfo<KaoQin> pageInfo = new PageInfo<KaoQin>(list);


        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("endPage",pageInfo.getNavigateLastPage());
        map.put("curPage",kaoQin.getPage());
        map.put("data",pageInfo.getList());
        return map;
    }

    //修改
    @Override
    public  HashMap<String, Object> updateByPrimaryKeySelective(KaoQin record) {
        int n =kaoQinMapper.updateByPrimaryKeySelective(record);
        HashMap<String, Object> map = new HashMap<>();
        map.put("info","修改失败");
        if (n > 0){
            map.put("info","修改成功");
        }

        return map;
    }

    //删除
    @Override
    public HashMap<String, Object> deleteByPrimaryKey(Long kId) {
        int n = kaoQinMapper.deleteByPrimaryKey(kId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("info","删除失败");
        if (n > 0){
            map.put("info","删除成功");
        }

        return map;
    }


    //添加
    @Override
    public HashMap<String, Object> insertSelective(KaoQin record) {
        int n = kaoQinMapper.insertSelective(record);
        HashMap<String, Object> map = new HashMap<>();
        map.put("info","新增失败");
        if (n > 0){
            map.put("info","新增成功");
        }

        return map;
    }

    @Override
    public HashMap<String, Object> selectAllByName(Student student) {

        //返回符合前端输入名字的全部学生记录
        List<Student> students = studentMapper.selectAllByName("%"+student.getsName()+"%");
        //对应的学号记录
        List<Integer> sidList = new ArrayList<>();
        for (Student student1 : students) {
            sidList.add(student1.getsId());
        }

        System.out.println("学号集合------------------------"+sidList);

        List<KaoQin> kaoqinList = new ArrayList<>();



        //根据学号查询考勤数据
        for (Integer sid : sidList) {
            List<KaoQin> kaoQins = kaoQinMapper.selectBySid(sid);
            for (KaoQin kaoQin : kaoQins) {
                SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" );
                String qTime = sdf1.format(kaoQin.getqTime());
                kaoQin.getStudent().setsSex(qTime);
                kaoqinList.add(kaoQin);
            }

        }
        System.out.println("获取所有的记录------"+kaoqinList);
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(student.getPage(),student.getRow());

        // 3 创建分页对象
        PageInfo<KaoQin> pageInfo = new PageInfo<KaoQin>(kaoqinList);

        List<KaoQin> listResult = pageInfo.getList();

        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        map.put("endPage",pageInfo.getNavigateLastPage());
        map.put("curPage",student.getPage());
        map.put("data",pageInfo.getList());

        return map;
    }
}
